<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;

class IndexController extends AdminBaseController
{

    public function _initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }
        parent::_initialize();
    }
    /**
     * 后台首页
     */
    public function index()
    {
        $adminMenuModel = new AdminMenuModel();
        //获取登录者id
        $uid=cmf_get_current_admin_id();
        //判断是不是超级管理员
        //$menus = cache('admin_menus_' . cmf_get_current_admin_id(), '', null, 'admin_menus');
        //if(empty($menus)){
            if($uid == 1){
                $menus = $adminMenuModel->menuTree();
                cache('admin_menus_' . cmf_get_current_admin_id(), $menus, null, 'admin_menus');
            }else{
                $uinfo=get_operator_data($uid);
                $menus_list=Db::name('AuthAccess')->where('role_id='.$uinfo['role_type'])->select()->toarray();
                $menus=array();
                foreach ($menus_list as $k=>$v){
                    //在admin_menu里面的数据
                    $adminMenuInfo=Db::name('AdminMenu')->where(array('id'=>$v['amid']))->find();
                    if($adminMenuInfo['parent_id'] == 0){
                        $app    = $adminMenuInfo['app'];
                        $controller  = $adminMenuInfo['controller'];
                        $action = $adminMenuInfo['action'];
                        $params = $adminMenuInfo['param'];
                        
                        if (strpos($app, 'plugin/') === 0) {
                            $pluginName = str_replace('plugin/', '', $app);
                            $url        = cmf_plugin_url($pluginName . "://{$controller}/{$action}{$params}");
                        } else {
                            $url = url("{$app}/{$controller}/{$action}{$params}");
                        }                        
                        $menus[$v['amid'].$adminMenuInfo['app']]=array(
                            'icon'=>$adminMenuInfo['icon'],
                            'ids'=>$v['amid'],
                            'id'=>$v['amid'].$adminMenuInfo['app'],
                            'name'=>$adminMenuInfo['name'],
                            'parent'=>$adminMenuInfo['parent_id'],
                            'url'=>$url,
                            'lang'=>strtoupper($adminMenuInfo['app'].'_'.$adminMenuInfo['controller'].'_'.$adminMenuInfo['action'])
                        );
                    }
                }
                if($menus){
                    foreach ($menus as $km=>$vm){
                        $items=array();
                        foreach ($menus_list as $ks=>$vs){
                             $adminInfo=Db::name('AdminMenu')->where(array('id'=>$vs['amid']))->find();
                            $app1         = $adminInfo['app'];
                            $controller1  = $adminInfo['controller'];
                            $action1      = $adminInfo['action'];
                            $params1      = $adminInfo['param'];
                            if (strpos($app1, 'plugin/') === 0) {
                                $pluginName1 = str_replace('plugin/', '', $app1);
                                $url1        = cmf_plugin_url($pluginName1 . "://{$controller1}/{$action1}{$params1}");
                            } else {
                                $url1 = url("{$app1}/{$controller1}/{$action1}{$params1}");
                            }
                            if($vs['pid'] == $vm['ids']){
                                $items[$vs['amid'].$adminInfo['app']]=array(
                                    'icon'=>$adminInfo['icon'],
                                    'ids'=>$vs['amid'],
                                    'id'=>$vs['amid'].$adminInfo['app'],
                                    'name'=>$adminInfo['name'],
                                    'parent'=>$vs['pid'],
                                    'url'=>$url1,
                                    'lang'=>strtoupper($adminInfo['app'].'_'.$adminInfo['controller'].'_'.$adminInfo['action'])
                                );
                                $menus[$vm['id']]['items']=$items;
                            }
                        }
                    }
                }
                cache('admin_menus_' . cmf_get_current_admin_id(), $menus, null, 'admin_menus');       
       }
        //}
      //print_r($menus);
        $this->assign("menus", $menus);       
        return $this->fetch();
    }
}
